/*
 * @Author: delin66
 * @Date: 2024-12-11 17:27:08
 * @LastEditors: delin66
 * @LastEditTime: 2024-12-18 23:21:48
 * @Descripttion:
 */
import { getMyReserveList, getRoomList, reserveRoom, updateRoomStatus } from '@/api/meeting';
import { ElMessage } from 'element-plus';
import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useRoomReserveStore = defineStore('roomReserve', () => {
  interface RoomReservation {
    createTime: string;
    updateTime: string;
    createBy: string;
    updateBy: string;
    id: number;
    roomId: number;
    userId: number;
    enterpriseId: number;
    startTime: string;
    endTime: string;
    remark: string;
    status: number;
  }

  interface roomList {
    createTime: string;
    updateTime: string;
    createBy: string;
    updateBy: string;
    id: number;
    roomNumber: string;
    roomName: string;
    maxPeople: number;
    roomDescription: string;
    status: number;
    calendarVisible: boolean;
    roomReservations: RoomReservation[];
  }
  interface form {
    roomId: number;
    userId: number;
    enterpriseId: number;
    startTime: string;
    endTime: string;
    remark: string;
  }

  const roomReserveList = ref<roomList[]>([]);
  const myReserve = ref<RoomReservation[]>([]);
  const reserveForm = ref<form>({
    roomId: 0,
    userId: 0,
    enterpriseId: 0,
    startTime: '',
    endTime: '',
    remark: '',
  });

  const filterValues = ref({
    roomName: null,
    status: null,
  });
  const addRoomReserve = (data: form) => {
    // 添加预约
    reserveRoom(data).then((res) => {
      if (res.code !== 200) {
        ElMessage.closeAll();
        ElMessage.error(res.msg);
        return;
      }
      ElMessage.success("提交预约成功！")
      getRoomList({
        current: 1,
        size: 10,
        ...filterValues.value
      }).then((res) => {
        roomReserveList.value = res.data.records;
      });
      getMyReserveList({
        current: 1,
        size: 20,
      }).then((res) => {
        myReserve.value = res.data.records;
      });
    })
  };
  //修改预约状态
  const cancelReserve = (id: number,status: number) => {
    updateRoomStatus({
      id: id,
      status: status,
    }).then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.msg);
        return;
      }
      // getMyReserveList({
      //   current: 1,
      //   size: 13,
      // }).then((res) => {
      //   myReserve.value = res.data.records;
      // });
      // getRoomList({
      //   current: 1,
      //   size: 10,
      //   ...filterValues.value
      // }).then((res) => {
      //   roomReserveList.value = res.data.records;
      // })

    })
    ElMessage.success({
      message: '取消预约成功！'
    });
  };
  return {
    roomReserveList,
    myReserve,
    reserveForm,
    filterValues,
    addRoomReserve,
    cancelReserve
  };
});
